/* fafamta.p - FIXED ASSET MAINTENANCE - Depreciation Books                  */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: fafamta.p 84345 2018-09-28 14:28:28Z s5a                          $: */
/* REVISION: 9.1      LAST MODIFIED: 10/26/99   BY: *N021* Pat Pigatti       */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane  */
/* REVISION: 9.1      LAST MODIFIED: 05/17/00   BY: *M0LJ* Vihang Talwalkar  */
/* REVISION: 9.1      LAST MODIFIED: 07/28/00   BY: *N0BX* Arul Victoria     */
/* REVISION: 9.1      LAST MODIFIED: 08/14/00   BY: *N0L0* Jacolyn Neder     */
/* REVISION: 9.1      LAST MODIFIED: 12/18/00   BY: *M0Y3* Veena Lad         */
/* REVISION: 9.1      LAST MODIFIED: 01/02/01   BY: *M0YM* Veena Lad         */
/* Revision: 1.23.1.15     BY: Alok Thacker       DATE: 03/13/02 ECO: *M1NB* */
/* Revision: 1.23.1.16     BY: Manish Dani        DATE: 12/20/02 ECO: *M1YW* */
/* Revision: 1.23.1.17     BY: Rajesh Lokre       DATE: 04/03/03 ECO: *M1RX* */
/* Revision: 1.23.1.18     BY: Veena Lad          DATE: 05/21/03 ECO: *N2FY* */
/* Revision: 1.23.1.20     BY: Paul Donnelly (SB) DATE: 06/26/03 ECO: *Q00C* */
/* Revision: 1.23.1.21     BY: Veena Lad          DATE: 10/17/03 ECO: *N2LF* */
/* Revision: 1.23.1.22     BY: Dorota Hohol       DATE: 10/27/03 ECO: *P138* */
/* Revision: 1.23.1.23     BY: Robert Jensen      DATE: 09/02/04 ECO: *N2XK* */
/* Revision: 1.23.1.28     BY: Vandna Rohira      DATE: 12/17/04 ECO: *P2Z5* */
/* Revision: 1.23.1.33     BY: Sushant Pradhan    DATE: 09/22/05 ECO: *P421* */
/* Revision: 1.23.1.34     BY: Austin Xie         DATE: 05/23/06 ECO: *R057* */
/* Revision: 1.23.1.35     BY: Austin Xie         DATE: 06/13/06 ECO: *R067* */
/* Revision: 1.23.1.36     BY: Dilip Manawat      DATE: 09/19/06 ECO: *P56R* */
/* Revision: 1.23.1.37     BY: Deepak Taneja      DATE: 07/13/07 ECO: *P5RQ* */
/* Revision: 1.23.1.38     BY: Jean Miller        DATE: 09/13/07 ECO: *R0C6* */
/* Revision: 1.23.1.39     BY: Antony LejoS       DATE: 05/20/08 ECO: *P6TG* */
/* Revision: 1.23.1.40     BY: Katie Hilbert      DATE: 04/05/10 ECO: *Q3ZM* */
/* REVISION: 23537     LAST MODIFIED: 10/19/12    BY: ddo                     */
/*-Revision end---------------------------------------------------------------*/

/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/
{us/bbi/mfdeclre.i}

/* Common API Constants and Variables */
{us/mf/mfaimfg.i}

/* EXTERNAL LABEL INCLUDE */
{us/bbi/gplabel.i}
{us/gp/gprunpdf.i "mcpl" "p"}
{us/fa/fapsmt.i}
{us/px/pxpgmmgr.i}
{us/px/pxphdef.i pccalcxr}

/* Post Overridden Depreciation Procedures */   
{us/fa/fapodpx.i }
{us/gp/gpoidfcn.i}  /* Defines nextOidValue() function */

define input parameter l_firstcost      like fa_puramt   no-undo.
define input-output parameter p-status  as character format "x(25)" no-undo.
define input-output parameter l_newbk   like mfc_logical no-undo.
define input-output parameter l_astpost like mfc_logical no-undo.
define input-output parameter l_cost    like fa_puramt   no-undo.
define parameter buffer fa_mstr for fa_mstr.

/* --------------------  DEFINE QUERY  ------------------- */
define query q_fab_det
   for fab_det
   scrolling.

/* -----------------  STANDARD VARIABLES  ---------------- */
define variable perform-status as character format "x(25)"
   initial "first" no-undo.
define variable p-skip-update  like mfc_logical no-undo.
define variable l-rowid        as rowid     no-undo.
define variable l-delete-it    like mfc_logical no-undo.
define variable l-del-rowid    as rowid     no-undo.
define variable l-top-rowid    as rowid     no-undo.
define variable lines          as integer   initial 10 no-undo.
define variable l-found        like mfc_logical no-undo.
define variable pos            as integer   no-undo.
define variable l-first        like mfc_logical no-undo.
define variable l-error        like mfc_logical no-undo.
define variable l-title        as character no-undo.
define variable l_ovramt       like fab_ovramt no-undo.  
define variable l_delta        like mfc_logical initial no no-undo.


/* ------------------  BUTTON VARIABLES  ----------------- */
define button b-add    label "Add".
define button b-delete label "Delete".
define button b-adjust label "Adjust".
define button b-detail label "Detail".
define button b-audit  label "Audit".

define button b-find   label "Find".
define button b-accts  label "Accts".

define button b-uop    label "UOP".
define button b-end    label "End".

/* -------------  STANDARD WIDGET VARIABLES  ------------- */
define variable l-focus         as widget-handle no-undo.
define variable w-frame         as widget-handle no-undo.

/* ------------------  SCREEN VARIABLES  ----------------- */
define new shared variable l-fa-id         as character     format "x(8)" no-undo.
define new shared variable l-id            as character     format "x(12)" no-undo.
define new shared variable l-seq           as integer       format "9999" no-undo.
define new shared variable l-type          as character     format "x(10)" no-undo.
define new shared variable l-fabk-id       as character     format "x(4)" no-undo.

define shared     variable l-flag          like mfc_logical no-undo.
define variable l-primary       like mfc_logical initial no no-undo.
define variable l-primary-bk    like fabk_id no-undo.
define variable l-post          like mfc_logical no-undo.
define new shared variable l-book          like fabk_id no-undo.
define new shared variable l-bookdesc      like fabk_desc no-undo.

define variable l_amt_fmt       as character     no-undo.
define variable l_bmt_fmt       as character     no-undo.
define variable mc-error-number like msg_nbr no-undo.
define variable rndmthd         like rnd_rnd_mthd no-undo.


define variable l_net           as decimal       format "->>>,>>>,>>9.99<"
   no-undo.
define variable l-curr          like currency.currencycode no-undo.                                                 
define variable l-err-nbr       as integer       format ">>>9" initial 0
   no-undo.
define variable l_asd_seq       as integer       format "9999" no-undo.
define variable l_acc           as decimal       format "->,>>>,>>9.99"
   no-undo.
define variable l_dep           as decimal       format "->>>,>>>,>>9.99<"
   no-undo.
define variable l_dispose       like mfc_logical no-undo.
define variable l-cal           as character     format "x(8)" no-undo.
define variable l-basis-amt     like fab_amt no-undo.
define variable l-book-screen   as character     no-undo.
define variable correct-yn      like mfc_logical initial yes no-undo.
define variable l-basis-amt-fmt as character     format "x(30)" no-undo.
define variable l_net_fmt       as character     format "x(30)" no-undo.
define variable l-asof-date     as character     format "9999-99" no-undo.
define variable l-famt-id       like famt_id no-undo.
define variable l-fab-life      like fab_life no-undo.
define variable l-period        as character     format "x(6)" no-undo.
define variable l-table         like mfc_logical no-undo.
define variable l-famtr-id      like fab_famtr_id no-undo.
define variable l-uplife        like fab_uplife no-undo.
define variable l-upper         like fab_upper no-undo.
define variable l-um            like fab_um no-undo.
define variable fab_post_oriden  like mfc_logical no-undo.  
define variable l_data_exist     like mfc_logical no-undo.    

define variable l_prim_cost     like fa_puramt no-undo.
define buffer bafab_det for fab_det.

/* EXCHANGE RATE DEFINIATION                                                  */
define variable l-bc-rate            like fab_bc_exr_rate no-undo.
define variable l-bc-ratescale       like fab_bc_exr_ratescale no-undo.
define variable l-sc-rate            like fab_sc_exr_rate no-undo.
define variable l-sc-ratescale       like fab_sc_exr_ratescale no-undo.
define variable vErrorNumber         as integer no-undo.

define variable l-statutory_curr     like glt_curr no-undo.
define variable l-statpropertyisstat as logical no-undo.
define buffer bfab_det for fab_det.
define variable hQADFinancialsLib as handle    no-undo.
define variable l-primary-curr    as character no-undo.  
define variable l_primary_book    as character no-undo. 
define variable l-rnd-mthd        like gl_rnd_mthd no-undo.

/* Local Vaibels used in API                                                 */
define variable l-sv-fabkid       as character no-undo.
define variable l-sv-fabdate      as date      no-undo.
define variable l-sv-fabmtid      as character no-undo.
define variable l-sv-fablife      as decimal   no-undo.
define variable l-sv-fabasisamt   as decimal   no-undo.
define variable l-sv-booksalvamt  as decimal   no-undo.
define variable l-sv-fabbcexrate  as decimal   no-undo.
define variable l-sv-fabscexrate  as decimal   no-undo.

define buffer bf_fab_det for fab_det.  
define buffer bf_fabk_mstr for fabk_mstr.  

/* Fixed Asset API dataset definition */
{us/fa/fadsfamt.i "reference-only"}

/* ---------------  PRE PROCESSING INCLUDE  -------------- */
/* FUNCTION TO VALIDATE ESTIMATED LIFE */
{us/fa/falffx.i}

/* VARIABLES FOR GPGLEF1.I */
{us/gp/gpglefv.i}

/* VARIABLES FOR FA PERSISTENT PROCEDURES */
{us/gp/gprunpdf.i "fapl" "p"}

/* ------------------  FRAME DEFINITION  ----------------- */
/* ADDED SIDE-LABELS PHRASE TO FRAME STATEMENT              */
if c-application-mode <> "API" then 
do:
   define frame f-button
      b-add    at 1
      b-adjust at 10
      b-accts  at 19
      b-detail at 28
      b-audit  at 37
      b-uop    at 46
      b-find   at 55
      b-delete at 64
      b-end    at 72
      with no-box overlay three-d side-labels width 80.

   /* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME*/
   clearFrameRegistration(frame f-button:handle).
   
   /* SET EXTERNAL LABELS */
   setFrameLabels(frame f-button:handle).

   assign
      l-focus               = b-add:handle in frame f-button
      b-add:width           = 8
      b-add:private-data    = "add"
      b-delete:width        = 8
      b-delete:private-data = "delete"
      b-adjust:width        = 8
      b-adjust:private-data = "adjust"
      b-accts:width         = 8
      b-accts:private-data  = "accts"
      b-detail:width        = 8
      b-detail:private-data = "detail"
      b-audit:width         = 8
      b-audit:private-data  = "audit"
      b-find:width          = 8
      b-find:private-data   = "find"
      b-uop:width           = 8
      b-uop:private-data    = "uop"
      b-end:width           = 8
      b-end:private-data    = "end".

   define frame f-1
      skip(.4)
      fab_fabk_id                                
      fab_date                                  
      l-famt-id                                  
      l-fab-life                                 
      l-basis-amt                                
      l_net       column-label "Net Value"
      l-curr      column-label "Curr"
      l-asof-date column-label "As Of"
      format "9999-99"
      with three-d overlay 3 down scroll 1 width 80.


   /* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME*/
   clearFrameRegistration(frame f-1:handle).

   /* SET EXTERNAL LABELS */
   setFrameLabels(frame f-1:handle).

   run ip-framesetup.

   w-frame = frame f-1:handle.
   {us/bbi/gprun.i ""fafmtut.p""
      "(w-frame)"}

end. /* c-application-mode <> "API" */


        
if c-application-mode = "API" then 
do on error undo, return:

   /* Get Handle of API Controller */
   {us/bbi/gprun.i ""gpaigach.p"" "(output ApiMethodHandle)"}

   if not valid-handle(ApiMethodHandle) then 
   do:
      /* API Error */
      {us/bbi/pxmsg.i &MSGNUM=10461 &ERRORLEVEL=4}
      return.
   end.

   /* Get the Fixed Asset API dataset from the controller */
   run getRequestDataset in APIMethodHandle
      (output dataset dsFixedAssetMaster bind).

end. /* c-application-mode = "API" */

lines = 03.
l_delta = yes.

if p-status = "add" then 
do:
   if c-application-mode <> "API" then
      frame f-1:visible = true.
   return.
end. /* IF p-status = "add" */



define new shared temp-table dep no-undo like fabd_det.

/* FETCHES THE Statutory CURRENCY */
{us/gp/gprunp.i "fapl" "p" "fa-getstatutorycurrency"
      "(output l-statutory_curr,
        output l-statpropertyisstat)"}
        
/* SET CURRENCY DEPENDENT ROUNDING FORMATS */
for first gl_ctrl
   where gl_ctrl.gl_domain = global_domain
   no-lock:
end. /* FOR FIRST gl_ctrl */

/* Set primary book currency */
for first fabk_mstr where fabk_mstr.fabk_primary = yes 
   and fabk_mstr.fabk_domain = global_domain 
   and fabk_post = yes no-lock,
   first cu_mstr of fabk_mstr no-lock:
   assign 
      l-primary-curr = cu_mstr.cu_curr
      l-primary-bk   = fabk_id.
end.

if c-application-mode <> "API" then 
do:
   assign
      l-basis-amt-fmt = l-basis-amt:format
      l_net_fmt       = l-basis-amt:format.

   {us/bbi/gprun.i ""gpcurfmt.p""
      "(input-output l-basis-amt-fmt,
        input        gl_rnd_mthd)"}
   
   {us/bbi/gprun.i ""gpcurfmt.p""
      "(input-output l_net_fmt,
        input        gl_rnd_mthd)"}
   
   assign
      l-basis-amt:format = l-basis-amt-fmt
      l_net:format       = l_net_fmt. 
end. /* end of c-application-mode <> "API" */


/* -------------  END PRE PROCESSING INCLUDE  ------------ */

  
for each bf_fab_det
   where bf_fab_det.fab_domain = global_domain 
     and bf_fab_det.fab_fa_id = fa_mstr.fa_id
use-index fab_fa_id
no-lock:

   fab_post_oriden = no.
   for first bf_fabk_mstr
      where bf_fabk_mstr.fabk_domain = global_domain
        and bf_fabk_mstr.fabk_id     = bf_fab_det.fab_fabk_id
   no-lock:
      /* Default value from book maintenance */
      fab_post_oriden = bf_fabk_mstr.fabk_post_overridden.
   end. /* FOR FIRST fabk_mstr */
   
   run ip-isdata-fab-det       
       (input bf_fab_det.fab_fa_id,
        input bf_fab_det.fab_fabk_seq,
        input bf_fab_det.fab_fabk_id,
        output l_data_exist).
   
   if l_data_exist = no and fab_post_oriden = yes then do:
      run ip-savedata-fab-det       
          (input recid(bf_fab_det),
           input fab_post_oriden).
   end.
  
end.  


if c-application-mode <> "API" then 
do:
   open query q_fab_det
      for each fab_det
      where fab_domain = global_domain and  fab_fa_id = fa_mstr.fa_id
      and   fab_resrv = 0
      use-index fab_fa_id
      no-lock.
   get first q_fab_det no-lock.
end. /* c-application-mode <> "API" */
else 
do:
   assign
      perform-status = "next".
end. /* c-application-mode = "API" */
if fa_mstr.fa_disp_dt <> ?
then
   l_dispose = yes.

main-loop:
do while perform-status <> "end" on error undo:
   if perform-status = ""
      and p-status = "first-one"
   then
      return.

   if p-status = "end"
   then
      perform-status = "end".

   run ip-query
      (input-output perform-status,
      input-output l-rowid).
   
   if perform-status = "delete"
   then do:
      run ip-lock
         (input-output perform-status).
      perform-status = "first".
      run ip-predisplay.
   end. /* IF PERFORM-STATUS = "delete" */
   if p-status = "first-one"
      and not available fab_det
   then do:
      if c-application-mode <> "API" then
         frame f-1:visible = true.
      return.
   end. /* IF p-status = "first-one"  */
   
   /* assign perform-status = "". */
   /* ---------------  AFTER QUERY INCLUDE  --------------- */
   assign
      l-type   = if available fab_det
                then
                    fab_fabk_id
                 else
                    l-type
      l-id     = if available fab_det
                 then
                    fab_fa_id
                 else
                    l-id
      l-seq    = if available fab_det
                 then
                    fab_fabk_seq
                 else
                  l-seq
      l_ovramt = if available fab_det
                 then
                   fab_ovramt
                 else
                    0.

   for first fabk_mstr
      where fabk_domain = global_domain
      and   fabk_id     = fab_fabk_id
      no-lock:
   end. /*FOR FIRST fabk_mstr*/
   if available fabk_mstr then
   do:
      assign
         l-post     = fabk_post
         l-book     = fabk_id
         l-bookdesc = fabk_desc.
      for first cu_mstr where cu_mstr.oid_cu_mstr = fabk_mstr.oid_cu_mstr no-lock:
         l-curr = cu_mstr.cu_curr.
      end.
      /* rounding format for book currency */
      if c-application-mode <> "API" then
      do:
         if l-curr <> '' and l-curr <> ?
         then do:
            {us/gp/gprunp.i "mcpl" "p" "mc-get-rnd-mthd"
            "(input l-curr,
              output l-rnd-mthd,
              output l-err-nbr)"}
            if l-err-nbr <> 0 then 
            do:
               return.
            end.
            {us/bbi/gprun.i ""gpcurfmt.p""
            "(input-output l_net_fmt,
              input        l-rnd-mthd)"}
            {us/bbi/gprun.i ""gpcurfmt.p""
            "(input-output l-basis-amt-fmt,
              input        l-rnd-mthd)"}
         end.
      end. /* c-application-mode <> "API" */
   end. /* available fabk_mstr */
      

   /* DISPLAY-EDITS */ 
   if c-application-mode <> "API" then
      assign 
         l_net:format in frame f-1       = l_net_fmt 
         l-basis-amt:format in frame f-1 = l-basis-amt-fmt.
   
      
       

   /* -------------  END AFTER QUERY INCLUDE  ------------- */

   /* ----------------------  ADJUST  --------------------- */    
   if perform-status = "adjust" or
      (c-application-mode = "API" and perform-status = 'update' and ttFixedAssetBooks.bookbutton = "Adjust") or
      (c-application-mode = "API" and perform-status = 'add' and ttFixedAssetBooks.bookbutton = "Adjust")
   then do:
      p-status = "display".
      if session:display-type = "gui" and c-application-mode <> "API"
      then
         assign
            fab_fabk_id:bgcolor = 8
            fab_fabk_id:fgcolor = 0.
      l_cost = 0.
      if available fab_det then
         assign
            l-fa-id   = fab_fa_id
            l-fabk-id = fab_fabk_id.
   
      {us/bbi/gprun.i ""fafamtg.p""
         "(input-output l_astpost,
           input-output l_cost)"}        
        
      /* CREATE tt_pstast RECORDS WITH DIFFERENCE OF COST 
      FOR DELTA YES OR NO AND ACQUISITION COST IS POSTED EARLIER */
      if c-application-mode <> "API" then
         assign l-sv-fabkid = fab_det.fab_fabk_id
            l-fabk-id   = l-sv-fabkid. /* debug */
      else assign l-sv-fabkid = fab_fabk_id.
      if l_cost <> 0
         and can-find(fa_mstr
         where fa_mstr.fa_domain    = global_domain
         and   fa_mstr.fa_id        = l-fa-id
         and   fa_mstr.fa_acq_post  = yes)
      then do:
         if not can-find (first tt_pstast
            where tt_asset = l-fa-id
            and   tt_book  = l-sv-fabkid)
         then do:
            create tt_pstast.
            assign 
               tt_pstast.tt_asset = l-fa-id
               tt_pstast.tt_book  = l-sv-fabkid
               tt_pstast.tt_effdt = today
               tt_pstast.tt_cost  = l_cost.

            run ip-delta
               (input l_delta,
               input l-primary-bk,
               input l-sv-fabkid,
               input l-fa-id,
               input l_cost).

         end. /* IF NOT CAN-FIND (FIRST tt_pstast */
         else 
         do:
            find first tt_pstast
               where tt_asset = l-fa-id
               and   tt_book  = l-sv-fabkid
               no-lock no-error.
            if available tt_pstast
            then do:
               assign
                  tt_pstast.tt_effdt = today
                  tt_pstast.tt_cost  = tt_pstast.tt_cost + l_cost.
            end. /* IF AVAILABLE tt_pstast */

            run ip-delta
               (input l_delta,
               input l-primary-bk,
               input l-sv-fabkid,
               input l-fa-id,
               input l_cost).

         end. /* ELSE DO */
      end. /* IF l_cost <> 0 and can-find(fa_mstr */

      assign
         p-status       = ""
         perform-status = "first".
      /* In creating a fixed asset with specified books info, */
      /* change status to update the fixed asset book details */
      if c-application-mode = "API" then 
         perform-status = "update". /* debug */
      
      run ip-displayedits.

   end. /* IF perform-status = "adjust" */
   /* --------------------  END ADJUST  ------------------- */
   /* ----------------------  Accts  ---------------------- */
   if perform-status = "accts" then
   do:
      if l-post = no then 
      do:
         {us/bbi/pxmsg.i &MSGNUM=7447 &ERRORLEVEL=4}
      end.
      else 
      do:

         assign
            p-status = "display".
         if session:display-type = "gui" and c-application-mode <> "API" then
            assign
               fab_fabk_id:bgcolor = 8
               fab_fabk_id:fgcolor = 0.
         {us/bbi/gprun.i ""fafamtf.p""}
         assign
            p-status       = ""
            perform-status = "first".
         run ip-displayedits.
      end.
   end.
   /* --------------------  End Accts  -------------------- */

   /* ----------------------  DETAIL  --------------------- */
   if perform-status = "detail"
   then do:
      p-status = "display".
      if session:display-type = "gui" and c-application-mode <> "API"
      then
         assign
            fab_fabk_id:bgcolor = 8
            fab_fabk_id:fgcolor = 0.

      {us/bbi/gprun.i ""fafamtd.p""}

      /* l-flag IS SET TO yes IN BATCH MODE IN fafamtd.p */
      /* FOR AN ERROR ENCOUNTERED.                       */
      if l-flag
      then
         return.

      assign
         p-status       = ""
         perform-status = "first".
      run ip-displayedits.
   end. /* if perform-status = "detail" */
   /* --------------------  END DETAIL  ------------------- */

   /* ----------------------  AUDIT  ---------------------- */
   if perform-status = "audit"
   then do:
      p-status = "display".
      if session:display-type = "gui" and c-application-mode <> "API"
      then
         assign
            fab_fabk_id:bgcolor = 8
            fab_fabk_id:fgcolor = 0.

      {us/bbi/gprun.i ""fafamtk.p""}

      assign
         p-status       = ""
         perform-status = "first".
      run ip-displayedits.
   end. /* IF perform-status = "audit"  */
   
   /* --------------------  END AUDIT  -------------------- */

   /* -----------------------  UOP  ----------------------- */
   if perform-status = "uop"
   then do:
      p-status = "display".
      if session:display-type = "gui"
      then
         assign
            fab_fabk_id:bgcolor = 8
            fab_fabk_id:fgcolor = 0.
      {us/bbi/gprun.i ""fafamtj.p""}

      assign
         p-status       = ""
         perform-status = "first".
      run ip-displayedits.
   end. /* IF perform-status = "uop"  */

   /* ---------------------  END UOP  --------------------- */

   /* ----------------------  DISPLAY  ---------------------- */
   run ip-predisplay.   
   /* ----------------------  ADD  ------------------------ */
   if (perform-status = "update"
      or perform-status = "add")
   then do:
      if perform-status = "update" 
      then do:
         assign  
            l-bc-rate      = if c-application-mode = "API" then ttFixedAssetBooks.fabBCExrRate else fab_bc_exr_rate 
            l-bc-ratescale = fab_bc_exr_ratescale
            l-sc-rate      = if c-application-mode = "API" then ttFixedAssetBooks.fabSCExrRate else fab_sc_exr_rate
            l-sc-ratescale = fab_sc_exr_ratescale.
         if (l-bc-rate = 0 or l-sc-rate = 0) then
         do:
            /* Get rates from TC to BC, and from TC to SC. */
            {us/gp/gprunp.i "fapl" "p" "fa-assign-rate"
                        "(input l-curr,
                           input fa_mstr.fa_acquis_effectivedate,
                           input base_curr,
                           input l-statutory_curr,
                           input yes,
                           output l-bc-rate,
                           output l-bc-ratescale,
                           output l-sc-rate,
                           output l-sc-ratescale,                       
                           output vErrorNumber)"}
            if vErrorNumber < 0 then
               return.  
         end.
      end. /* perform-status = "update"  */
        
              
      run ip-prompt.
        
        
      /* l-flag IS SET TO yes IN BATCH MODE IN PROCEDURE ip-prompt */
      /* FOR AN ERROR ENCOUNTERED.                                 */
      if l-flag
      then
         return.
         
      if perform-status = "undo"
      then do:
         perform-status = "first".
         next main-loop.
      end. /* IF perform-status = "undo"  */
      run ip-displayedits.
   end. /*  IF (perform-status = "update" .... */

   /* ----------------  SELECTION BUTTONS  ---------------- */
   if p-status <> "first-one"
      and p-status <> "end"
      and perform-status <> "first"
   then
      run ip-button
         (input-output perform-status).

   if fa_post
      and perform-status = "add"
   then do:
      /* CANNOT ADDED - ASSET HAS BEEN POSTED */
      {us/bbi/pxmsg.i &MSGNUM=13489 &ERRORLEVEL=4}
      perform-status = "first".
   end. /* if fa_post... */


   /* -------------  AFTER STRIP MENU INCLUDE  ------------ */
   if (perform-status = "update"
      or perform-status = "delete")
      and fa_post = yes
   then do:
      if perform-status = "delete"
      then do:
         /* CANNOT DELETE - DEPRECIATION POSTED FOR THIS BOOK */
         {us/bbi/pxmsg.i &MSGNUM=4230 &ERRORLEVEL=3}
      end. /* IF perform-status = "delete"  */
      else 
      do:
         /* DEPRECIATION HAS STARTED ON THIS ASSET YOU MAY    */
         /* NOT ADJUST DEPRECIATION                           */
         {us/bbi/pxmsg.i &MSGNUM=3198 &ERRORLEVEL=4}
      end. /* ELSE DO */
      perform-status = "".
   end.  /* IF (perform-status = "update"  */
   p-skip-update = no.
   if perform-status = "adjust" or
      (c-application-mode = "API" and perform-status = "" and
      ttFixedAssetBooks.bookbutton = "Adjust")
   then do:
      if fa_post = no
      then
         perform-status = "update".
      else
         if not can-find(first fabd_det
            where fabd_det.fabd_domain = global_domain 
            and   fabd_fa_id   = fab_fa_id
            and   fabd_fabk_id = fab_fabk_id)
         then do:
            /* DEPRECIATION SCHEDULE DOES NOT EXIST */
            {us/bbi/pxmsg.i &MSGNUM=3199 &ERRORLEVEL=2}
            perform-status = "first".
         end. /* THEN DO */
   end.  /* IF perform-status = "adjust" */
   if available fab_det
   then
      assign
         l-fa-id   = fab_fa_id
         l-fabk-id = fab_fabk_id.

   if perform-status = "find"
   then do:
      run ip-book
         (input-output l-rowid).
      if l-rowid <> ?
      then
         run ip-query
            (input-output perform-status,
            input-output l-rowid).
   end. /* IF PERFORM-STATUS = "FIND" */

   if perform-status = "uop"
   then do:
      for first famt_mstr
         where famt_mstr.famt_domain = global_domain
         and   famt_id               = fab_famt_id /* l-famt-id */
         and   famt_type             = "2"
         no-lock:
      end. /* FOR FIRST famt_mstr */
      if not available famt_mstr
      then do:
         /* UOP OPTION ONLY AVAILABLE FOR UNITS OF PRODUCTION METHODS */
         {us/bbi/pxmsg.i &MSGNUM=3200 &ERRORLEVEL=4}
         perform-status = "".
      end. /* IF NOT AVAILABLE */
   end.  /* IF "uop" */
   /* -----------  END AFTER STRIP MENU INCLUDE  ---------- */

   /* -----------------------  END  ----------------------- */
   if perform-status = "end"
   then do:
      if c-application-mode <> "API" then
         hide frame f-1 no-pause.
      delete PROCEDURE this-procedure no-error.
      leave.
   end.  /* IF perform-status = "end" */
   /* reset l-basis-amt */
   assign 
      l-basis-amt     = 0
      l-sv-fabasisamt = 0.
   if c-application-mode = "API" then assign perform-status = 'next'.

end. /* mainloop */

/* -------------  ADD / UPDATE / FIELD EDITS  ------------ */
PROCEDURE ip-prompt:
   if perform-status = "add"
   then do:
      if c-application-mode <> "API" then
         scroll from-current down with frame f-1.
      assign
         l_newbk  = yes
         l-rowid  = ?
         l_ovramt = 0.
   end. /* IF perform-status = "add" */

   assign
      l-table = no
      l-first = yes.
   if c-application-mode <> "API" then 
   do:
      CASE perform-status:
         when ("add")
   then do:
               /* ADDING NEW RECORD */
               {us/bbi/pxmsg.i &MSGNUM=1 &ERRORLEVEL=1}
            end. /* when (add) */
         when ("update")
         then 
            do:
               /* MODIFYING EXISTING RECORD */
               {us/bbi/pxmsg.i &MSGNUM=10 &ERRORLEVEL=1}
            end. /* when update */
         otherwise
         hide message no-pause.
      END CASE.
   
      ststatus = stline[3].
      status input ststatus.
   end. /* c-application-mode <> "API" */
   prompt-for-it:
   repeat:
      l-first = no.
      repeat:
         if c-application-mode <> "API" then 
         do:
            prompt-for
               fab_det.fab_fabk_id
               when (perform-status = "add")
               fab_det.fab_date
               l-famt-id
               with frame f-1.
            assign
               l-famt-id.
         end. /* c-application-mode <> "API" */
         /* Set the API default */
         /* Only for creation, skip modifying */
         if c-application-mode = "API" and ttFixedAssetBooks.operation = {&ADD} 
            and perform-status = 'add'then /* debug */
            assign
               {us/mf/mfaidflt.i ttFixedAssetBooks.bookMethod """"}.
         /* Set the screen-value variable either from the screen or the temp-table */
         assign
            l-sv-fabkid  = if c-application-mode <> "API" then
                          input frame f-1 fab_fabk_id
                       else if perform-status = "add" or ttFixedAssetBooks.operation = {&ADD} then
                          ttFixedAssetBooks.fabFabkId
                       else
                          fab_det.fab_fabk_id
            l-sv-fabdate = if c-application-mode <> "API" then
                              input frame f-1 fab_date
                           else ttFixedAssetBooks.fabDate
            l-sv-fabmtid = if c-application-mode <> "API" then
                               input frame f-1 l-famt-id
                            else ttFixedAssetBooks.bookMethod. 
         /* UOP method will be supported in termial model only. */
         if {us/bbi/gpiswrap.i} and c-application-mode <> "API" then
       do:
            if can-find (first famt_mstr where famt_mstr.famt_domain = global_domain
                     and   famt_id   = l-sv-fabmtid
                     and   famt_type = "2") then do:
               /* PROGRAM HAS LIMITED FUNCTIONALITY FOR UOP IN DESKTOP MODE */
               {us/bbi/pxmsg.i &MSGNUM=16103 &ERRORLEVEL=3}
               next-prompt l-famt-id with frame f-1.
               next.
            end.
         end.         
         /* Default from the schema - no longer works when assigning to a local variable */
         /* Only for creation, skip modifying */
         if l-sv-fabdate = ?  
            and (perform-status = 'add'and c-application-mode = "API" ) then /* debug */
            l-sv-fabdate = today.
         /* Only for creation, skip modifying */
         if l-sv-fabkid = "" 
            and (perform-status = 'add'and c-application-mode = "API" ) /* debug */
         then do:
            /* BLANK NOT ALLOWED */
            {us/bbi/pxmsg.i &MSGNUM=40 &ERRORLEVEL=3}
   
            /* l-flag IS SET TO yes IN BATCH MODE. */
            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt
                  fab_fabk_id
                  with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else 
            do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* if input frame f-1 fab_fabk_id = "" */
         /* FIELD EDIT FOR fab_fabk_id */
         l-error = no.
         if not can-find(fabk_mstr
            where fabk_mstr.fabk_domain = global_domain
            and fabk_id = l-sv-fabkid)
         then do:
            /* INVALID BOOK CODE */
            {us/bbi/pxmsg.i &MSGNUM=4214 &ERRORLEVEL=3}
            l-error = yes.
         end. /* if not can-find(fabk_mstr.... */
         
         if perform-status = "add"
            and can-find(first fab_det
            where fab_det.fab_domain = global_domain
            and   fab_fa_id   = fa_mstr.fa_id
            and   fab_fabk_id = l-sv-fabkid)
         then do:
            /* BOOK # ALREADY EXISTS FOR THIS ASSET */
            {us/bbi/pxmsg.i &MSGNUM=4254 &ERRORLEVEL=3
                     &MSGARG1="l-sv-fabkid"}
            l-error = yes.
         end. /* THEN DO */

         /* When adding any secondary posting book, verify */
         /* if Primary Posting book exists */

         if perform-status = "add"
            and can-find(first fabk_mstr
            where fabk_domain = global_domain
            and   fabk_id   = l-sv-fabkid
            and   fabk_post = yes
            and   fabk_primary = no)
         then do:
            l-primary = no.
            for each fab_det where fab_domain = global_domain
               and fab_fa_id = fa_mstr.fa_id :

               if fab_fabk_id = l-primary-bk then
                  l-primary = yes.

            end. /* FOR EACH FAB_DET */

            if not l-primary then 
            do:
               /* PRIMARY POSTING BOOK NOT ASSIGNED TO THIS ASSET */
               {us/bbi/pxmsg.i &MSGNUM=7449 &ERRORLEVEL=4}
               l-error = yes.
               undo, retry.
            end. /* IF NOT L-PRIMARY  */
         end. /* THEN DO */

         /* Create faba_det records */
         if perform-status = "add"
            and not can-find (first faba_det where faba_domain = global_domain
            and faba_fa_id = fa_id
            and faba_fabk_id = l-sv-fabkid )
         then do:
            if can-find (first facd_det no-lock
               where facd_domain   = global_domain
               and facd_facls_id = fa_facls_id
               and facd_fabk_id  = l-sv-fabkid )
            then do:
               for each facd_det
                  where facd_domain   = global_domain
                  and facd_facls_id = fa_facls_id
                  and facd_fabk_id  = l-sv-fabkid
                  no-lock:
                  create faba_det.
                  assign
                     faba_fa_id    = fa_id
                     faba_fabk_id  = l-sv-fabkid
                     faba_acctype  = facd_acctype
                     faba_glseq    = 0
                     faba_mod_user = global_userid
                     faba_mod_date = today
                     faba_domain   = global_domain.
                  if recid(faba_det)  = -1 then .
               end. /* for each facd_det */
            end. /* if can-find facd_det */
            else 
            do:
               /* FOR EACH RELATED CODE IN lngd_det */
               for each lngd_det no-lock
                  where lngd_dataset = "facd_det"
                  and   lngd_field = "facd_acctype"
                  and   lngd_lang = global_user_lang
                  break by lngd_key1:
                  create faba_det.
                  assign
                     faba_fa_id    = fa_id
                     faba_fabk_id  = l-sv-fabkid
                     faba_acctype  = lngd_key1
                     faba_glseq    = 0
                     faba_mod_user = global_userid
                     faba_mod_date = today
                     faba_domain   = global_domain.
                  if recid(faba_det) = -1 then .
               end. /* for each lngd_det  */
            end. /* else do - if not can-find facd_det */
         end. /* perform-status = add and not can-find faba_det */


         /* l-flag IS SET TO yes IN BATCH MODE. */
         if l-error
         then do:
            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt
                  fab_fabk_id
                  with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else 
            do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* IF l-error */
         /* END FIELD EDIT FOR fab_fabk_id */

         /* FIELD EDIT FOR fab_date */
         l-error = no.

         /* VALIDATE BASIS DATE IN GL/CUSTOM CALENDAR */
         /* BUT ONLY DISPLAY A WARNING SINCE FA POST  */
         /* CAN PERFORM "CATCH UP" FUNCTION.          */
         for first fabk_mstr
            where fabk_mstr.fabk_domain = global_domain
            and   fabk_id                = l-sv-fabkid
            no-lock:
            /* VALIDATE DATE IN CUSTOM CALENDAR */
            if fabk_calendar = ""
            then do:
               {us/bbi/gprun.i ""gpglef1.p""
                  "(input  ""FA"",
                    input  fa_mstr.fa_entity,
                    input  l-sv-fabdate,
                    output gpglef_result,
                    output gpglef_msg_nbr)"}
               /* ONLY DISPLAY A WARNING IF CLOSED PERIOD */
               if gpglef_result = 2
               then do:
                  /* CLOSED PERIOD */
                  {us/bbi/pxmsg.i &MSGNUM=gpglef_msg_nbr &ERRORLEVEL=2}
               end.
               else if gpglef_result <> 0
               then do:
                  /* INVALID PERIOD OR INACTIVE ENTITY */
                  {us/bbi/pxmsg.i &MSGNUM=gpglef_msg_nbr &ERRORLEVEL=3 &MSGAR1=fa_mstr.fa_entity}
                  l-error = yes.
               end.  /* IF gpglef_result = 2 */
            end.  /* IF fabk_calendar = "" */
            for first famt_mstr
               where famt_mstr.famt_domain  = global_domain
               and   famt_id                = l-sv-fabmtid
               and   famt_type              = "6"
               /* CUSTOM TABLE */
               no-lock:
               l-cal = if fabk_post
                  then ""
                  else fabk_calendar.
               {us/gp/gprunp.i "fapl" "p" "fa-get-per"
                  "(input  input frame f-1 fab_date,
                    input  l-cal,
                    output l-period,
                    output l-err-nbr)"}

               if l-err-nbr > 0
               then do:
                  {us/bbi/pxmsg.i &MSGNUM=l-err-nbr &ERRORLEVEL=3 &FIELDNAME=""fabDate""}
                  l-error = yes.
                  leave.
               end. /* IF l-err-nbr > 0 */

               if integer(substring(l-period,5,2)) > 13
               then do:
                  /* CUSTOM TABLE CANNOT BE USED WITH A     */
                  /* CALENDAR THAT HAS MORE THAN 13 PERIODS */
                  {us/bbi/pxmsg.i &MSGNUM=3197 &ERRORLEVEL=3 &FIELDNAME=""fabDate""}
                  l-error = yes.
               end. /* IF integer(substring(l-period,5,2)) > 13 */
            end.  /* FOR FIRST famt_mstr */
         end. /*FOR FIRST fabk_mstr*/

         /* l-flag IS SET TO yes IN BATCH MODE. */
         if l-error
         then do:
            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt
                  fab_date
                  with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else 
            do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end.  /* IF l-error */
         /* END FIELD EDIT FOR fab_date */

         /* FIELD EDIT FOR l-famt-id */
         l-error = no.
         for first famt_mstr
            where famt_mstr.famt_domain = global_domain
            and   famt_id = l-sv-fabmtid
            no-lock:
         end. /*FOR FIRST famt_mstr*/
         if not available famt_mstr
            or (available famt_mstr
            and not famt_active)
         then do:
            /* INVALID DEPRECIATION METHOD */
            {us/bbi/pxmsg.i &MSGNUM=4200 &ERRORLEVEL=3 &FIELDNAME=""bookMethod""}
            l-error = yes.
         end.

         else do:
            if famt_type = "2"
            then do:
               /* UOP */
               {us/bbi/gprun.i ""fafaut1.p""
                  "(input-output l-uplife,
                    input-output l-upper,
                    input-output l-famtr-id,
                    input-output l-um)"}
               if c-application-mode <> "API" then
                  assign
                     l-fab-life:screen-value = "0"
                     l-table                 = yes.
               else
                  assign
                     {us/mf/mfaidflt.i ttFixedAssetBooks.bookLife 0}
                     l-table = yes.              
               leave.
            end. /* if famt_type = "2" */
            if famt_type = "6"
            then
               /* CUSTOM TABLE */
               if c-application-mode <> "API" then
                  assign
                     l-table                 = yes
                     l-fab-life:screen-value = string(famt_elife).
               else
                  assign
                     l-table = yes
                     ttFixedAssetBooks.bookLife = famt_elife.
                     
         end. /* ELSE IF AVAILABLE famt_mstr */

         /* l-flag IS SET TO yes IN BATCH MODE. */
         if l-error
         then do:
            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt
                  l-famt-id
                  with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else 
            do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* IF l-error */
         /* END FIELD EDIT FOR l-famt-id */
         leave.
      end.
      if keyfunction(lastkey) = "end-error"
      then do:
         perform-status = "undo".
         return no-apply.
      end. /* if keyfunction(lastkey) */
      repeat:
         for each cu_mstr where cu_mstr.oid_cu_mstr = fabk_mstr.oid_cu_mstr no-lock:
            assign       
               l-curr = cu_mstr.cu_curr.
            if c-application-mode <> "API" then
            do:
               {us/gp/gprunp.i "mcpl" "p" "mc-get-rnd-mthd"
               "(input l-curr,
                 output l-rnd-mthd,
                 output l-err-nbr)"}
               if l-err-nbr = 0 then do:
                  {us/bbi/gprun.i ""gpcurfmt.p""
                  "(input-output l-basis-amt-fmt,
                    input        l-rnd-mthd)"}
               end.
               assign 
                  l-basis-amt:format in frame f-1 = l-basis-amt-fmt.
            end.
         end.
         if c-application-mode <> "API" then 
         do: 
            prompt-for
               l-fab-life
               when (not l-table)
               l-basis-amt
               with frame f-1.

            assign
               l-fab-life
               l-basis-amt.
         end. /* end of c-application-mode <> "API" */
         /* Set the API default */
         if c-application-mode = "API" and ttFixedAssetBooks.operation = {&ADD} and perform-status = "add" then /* debug */
            assign
               {us/mf/mfaidflt.i ttFixedAssetBooks.bookLife 0}
               {us/mf/mfaidflt.i ttFixedAssetBooks.bookCost 0}.

         assign
            l-sv-fablife    = if c-application-mode <> "API" then
                              input frame f-1 l-fab-life
                           else if not l-table then
                              ttFixedAssetBooks.bookLife
                           else
                              l-fab-life
            l-sv-fabasisamt = if c-application-mode <> "API" then
                                 input frame f-1 l-basis-amt
                              else
                                 ttFixedAssetBooks.bookCost.
         /* FIELD EDIT FOR l-fab-life */
         assign
            l-error   = no
            l-err-nbr = f-validate-life(l-sv-fabmtid,
                                        l-sv-fablife)
            l-error   = l-err-nbr > 0.

         if l-err-nbr > 0
         then do:
            {us/bbi/pxmsg.i &MSGNUM=l-err-nbr &ERRORLEVEL=3}
         end. /* IF l-err-nbr > 0 */

         /* l-flag IS SET TO yes IN BATCH MODE. */
         if l-error
         then do:
            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt
                  l-fab-life
                  with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else 
            do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* IF l-error */
         /* END FIELD EDIT FOR l-fab-life */

         /* FIELD EDIT FOR l-basis-amt */
         l-error = no.
         /* CHECK CURRENCY DEPENDENT ROUNDING FORMAT */
         /*
         {us/bbi/gprun.i ""gpcurval.p""
            "(input  input frame f-1 l-basis-amt,
              input  gl_ctrl.gl_rnd_mthd,
              output l-err-nbr)"}
         if l-err-nbr <> 0
         then
            l-error = yes.
         */
         l-err-nbr = 0.

         if l_ovramt > l-sv-fabasisamt
         then do:
            /* ACCUMULATED DEPRECIATION IS GREATER */
            /* THAN ADJUSTMENT AMOUNT              */
            {us/bbi/pxmsg.i &MSGNUM=3191 &ERRORLEVEL=3}
            l-error = yes.
         end. /* IF l_ovramt > l-sv-fabasisamt */

         /* l-flag IS SET TO yes IN BATCH MODE. */
         if l-error
         then do:
            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt
                  l-basis-amt
                  with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else 
            do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* IF l-error */

         /* END FIELD EDIT FOR l-basis-amt */

         leave.
      end.

      if keyfunction(lastkey) = "end-error"
      then
         next prompt-for-it.

      /* IS ALL INFO CORRECT */
      if c-application-mode <> "API" 
      then do:
         {us/bbi/pxmsg.i &MSGNUM=12 &ERRORLEVEL=1 &CONFIRM=correct-yn
               &CONFIRM-TYPE='LOGICAL'}
      end.
      else assign correct-yn = yes.

      if not correct-yn
      then do:
         l-error = yes.
         if perform-status = "add"
         then do:
            next-prompt
               fab_det.fab_fabk_id
               with frame f-1.
            next prompt-for-it.
         end.  /* IF perform-status = "add" */
         else
            if perform-status = "update"
            then do:
               next-prompt
                  fab_det.fab_date
                  with frame f-1.
               next prompt-for-it.
            end.  /* IF perform-status = "update" */
      end. /* IF NOT correct .. */

      /* GET ORIGINAL ASSET COST OR DIFFERENCE. THE ORIGINAL ASSET */
      /* COST MEANS COST OBTAINED WHEN ADDING AN ASSET, ADDING A   */
      /* POST BOOK TO EXISTING ASSET OR THE COST OF POST BOOK      */
      /* MODIFIED IMMEDIATELY AFTER ADDING AN ASSET/POST BOOK      */
      else        
         if fabk_post
         then do:
            if (perform-status     = "add"
               and l-basis-amt    <> 0)
               or  (perform-status = "update")
            then do:
               if perform-status = "add"
                  or l_newbk 
               then
                  /* GET COST OF POST BOOK ADDED */
                  l_cost = l-sv-fabasisamt.
               else do:
                  /* GET DIFFERENCE OF COST WHEN EXISTING POST BOOK IS MODIFIED */
                  for first bafab_det
                     where bafab_det.fab_domain  = global_domain
                     and   bafab_det.fab_fa_id   = l-fa-id
                     and   bafab_det.fab_fabk_id = l-sv-fabkid
                     no-lock:
                     l_cost = l-sv-fabasisamt - bafab_det.fab_amt.
                  end. /* FOR FIRST fab_det */
               end. /* ELSE DO */

               l_astpost = if l_cost <> 0
                  then yes
                  else no.
               if l_cost <> 0
                  and can-find(fa_mstr
                  where fa_mstr.fa_domain    = global_domain
                  and   fa_mstr.fa_id        = l-fa-id
                  and   fa_mstr.fa_acq_post  = yes)
               then do:
                  if not can-find(first tt_pstast
                     where tt_asset = l-fa-id
                     and   tt_book  = l-sv-fabkid)
                  then do:
                     if  perform-status = "add"
                        and can-find(fabk_mstr where fabk_mstr.fabk_domain = global_domain
                        and fabk_mstr.fabk_id     = l-sv-fabkid
                        and fabk_mstr.fabk_post   = yes
                        and fabk_mstr.fabk_delta  = yes)
                        and l-primary-bk   <> l-sv-fabkid
                     then do:
                        for each bafab_det
                           where bafab_det.fab_domain = global_domain
                           and bafab_det.fab_fa_id   = l-fa-id
                           and bafab_det.fab_fabk_id = l-primary-bk no-lock
                           break by bafab_det.fab_fabk_id:
                           /* IF DELTA YES AND SECONDARY POSTING BOOK ADDED THEN CALCULATE */
                           /* COST OF PRIMARY BOOK TO GET DIFFERENCE TO POST TO GL */
                           l_prim_cost = l_prim_cost + bafab_det.fab_amt.
                           if last-of(bafab_det.fab_fabk_id)
                              and l_prim_cost <> 0
                           then do:
                              create tt_pstast.
                              assign
                                 tt_pstast.tt_asset = l-fa-id
                                 tt_pstast.tt_book  = l-sv-fabkid
                                 tt_pstast.tt_effdt = today
                                 tt_pstast.tt_cost  = l_cost - l_prim_cost.
                           end. /* IF LAST-OF(bafab_det.fab_fabk_id) */
                        end. /* FOR EACH bafab_det */
                     end. /* IF perform-status = "add" */
                     else do:
                        create tt_pstast.
                        assign 
                           tt_pstast.tt_asset = l-fa-id
                           tt_pstast.tt_book  = l-sv-fabkid
                           tt_pstast.tt_effdt = today
                           tt_pstast.tt_cost  = l_cost.

                        run ip-delta
                           (input l_delta,
                           input l-primary-bk,
                           input l-sv-fabkid,
                           input l-fa-id,
                           input l_cost).
                     end. /* ELSE DO: */
                  end. /* IF NOT CAN-FIND(FIRST tt_pstast */
                  else do:
                     find first tt_pstast
                        where tt_asset = l-fa-id
                        and   tt_book  = l-sv-fabkid
                        no-lock no-error.
                     if available tt_pstast
                     then do:
                        assign
                           tt_pstast.tt_effdt = today
                           tt_pstast.tt_cost  = tt_pstast.tt_cost + l_cost.
                     end. /* IF AVAILABLE tt_pstast */

                     run ip-delta
                        (input l_delta,
                        input l-primary-bk,
                        input l-sv-fabkid,
                        input l-fa-id,
                        input l_cost).
                  end. /* ELSE DO */
               end. /* IF l_cost <> 0 */
            end. /* IF (perform-status) = "add" */
         end. /* IF fabk_post */
      if keyfunction(lastkey) = "end-error"
      then
         next prompt-for-it.
      leave.
   end. /* prompt-for-it */
   
   if keyfunction(lastkey) = "end-error"
   then do:
      if perform-status = "add"
      then do:
         /* RECORD NOT ADDED */
         {us/bbi/pxmsg.i &MSGNUM=1304 &ERRORLEVEL=1}
         get first q_fab_det
            no-lock.
         if available fab_det
         then do:
            assign
               perform-status = "first"
               l-rowid        = rowid(fab_det).
         end. /* IF AVAILABLE fab_det */
         else
            assign
               perform-status = ""
               l-rowid        = ?.
         if c-application-mode <> "API" then
            clear frame f-1 no-pause.
      end.
      else
         if available fab_det
         then
            assign
               perform-status = ""
               l-rowid        = rowid(fab_det).
      return.
   end.  /* IF keyfunction(lastkey) = "end-error" */ 
   run ip-lock
      (input-output perform-status).
END PROCEDURE.

/* -----------------------  LOCKING  ----------------------- */
PROCEDURE ip-lock:
   define input-output parameter perform-status as character no-undo.

   if perform-status = "add"
      or perform-status = "update"
      or perform-status = "delete"
   then
   ip-lock:
   do transaction on error undo:
      assign
         pos         = 0
         l-delete-it = yes.
      if c-application-mode <> "API" then 
      do:
         if available fab_det
         then
            get current q_fab_det
               no-lock.
         if available fab_det
            and current-changed fab_det
         then do:
            l-delete-it = yes.
            /* RECORD HAS BEEN MODIFIED SINCED YOU EDITED IT SAVE ANYWAY */
            {us/bbi/pxmsg.i &MSGNUM=2042 &ERRORLEVEL=1 &CONFIRM=l-delete-it
                     &CONFIRM-TYPE='LOGICAL'}
            if l-delete-it = no
            then do:
               hide message no-pause.
               run ip-displayedits.
               return.
            end.  /* IF l-delete-it = no */
            hide message no-pause.
         end.  /* IF available fab_det */
      end. /* c-application-mode <> "API" */
      else 
         find current fab_det no-lock no-error.
      if available fab_det
      then
      tran-lock:
      do while perform-status <> "":
         if c-application-mode <> "API" then
            get current q_fab_det exclusive-lock no-wait.
         else do:
            find current fab_det exclusive-lock no-wait.
         end.
         if locked fab_det
         then do:
            if pos < 3
            then do:
               assign
                  pos = pos + 1.
               pause 1 no-message.
               next tran-lock.
            end. /* IF pos < 3 */
            perform-status = "error".
         end.
         leave.
      end. /*while*/

      if (perform-status  = "update"
         or p-status    = "update")
         and l-delete-it = yes
      then
         run ip-update
            (input-output perform-status).
      if perform-status = "delete"
      then
         run ip-delete
            (input-output perform-status).

      if available fab_det
      then
         if c-application-mode <> "API" then
            get current q_fab_det no-lock.
         else
            find current fab_det no-lock.
      if perform-status = "add"
      then
         run ip-add
            (input-output perform-status).
      if perform-status = "undo"
      then
         undo ip-lock, return.
   end.
END PROCEDURE.

PROCEDURE ip-update:
   define input-output parameter perform-status as character no-undo.
   if p-status = "update"
   then
      p-status = "".
   run ip-assign
      (input-output perform-status).
   if perform-status = "undo"
   then
      return.
   perform-status = "open".
   run ip-query
      (input-output perform-status,
      input-output l-rowid).
   if c-application-mode <> "API" then 
   do:
      reposition q_fab_det to rowid l-rowid no-error.
      get next q_fab_det
         no-lock.
      assign
         perform-status = "first"
         l-rowid        = rowid(fab_det).
   end. /* c-application-mode <> "API" */
   return.
END PROCEDURE.

PROCEDURE ip-add:
   define input-output parameter perform-status as character no-undo.
   create fab_det. 
   fab_det.fab_domain = global_domain.
   run ip-assign
      (input-output perform-status).
   if perform-status = "undo"
   then
      return.
   perform-status = "open".
   run ip-query
      (input-output perform-status,
      input-output l-rowid).
   if c-application-mode <> "API" then 
   do:
      reposition q_fab_det to rowid l-rowid no-error.
      get next q_fab_det
         no-lock.
      assign
         perform-status = "first"
         l-rowid        = rowid(fab_det).
   end. /* c-application-mode <> "API" */
   return.
END PROCEDURE.

PROCEDURE ip-delete:
   define input-output parameter perform-status as character no-undo.
   l-delete-it = no.
   if c-application-mode <> "API" then
      /* PLEASE CONFIRM DELETE */
      {us/bbi/pxmsg.i &MSGNUM=11 &ERRORLEVEL=1 &CONFIRM=l-delete-it
               &CONFIRM-TYPE='LOGICAL'}
   else assign l-delete-it = yes.
   CASE l-delete-it:
      when yes
   then do:
            /* DO NOT ALLOW TO DELETE THE PRIMARY POSTING BOOK IF SECONDARY  */
            /* POSTING BOOK STILL EXISTS */

            if fab_det.fab_fabk_id = l-primary-bk then 
            do:
               if can-find( first fabd_det
                  where fabd_domain = global_domain
                  and  fabd_fa_id = fab_det.fab_fa_id
                  and  fabd_fabk_id <> l-primary-bk
                  and  ( not can-find (first fabk_mstr
                  where fabk_domain = global_domain
                  and fabk_id = fabd_fabk_id
                  and fabk_post = no)))
               then do:
                  {us/bbi/pxmsg.i &MSGNUM=7448 &ERRORLEVEL=4}
                  return.
               end. /* can-find first fabd_det  */
            end. /* fab_det.fab_fabk_id = l-primary-bk  */

            /* LOCKED-DELETE-EDIT */
            /* DELETE DEPRECIATION ARRAY FOR BOOK */
            for each fabd_det
               where fabd_det.fabd_domain = global_domain and  fabd_fa_id   =
               fab_det.fab_fa_id
               and   fabd_fabk_id = fab_det.fab_fabk_id
               exclusive-lock:
               delete fabd_det.
            end. /* FOR EACH fabd_det */

            for each faba_det
               where faba_det.faba_domain = global_domain 
               and faba_fa_id = fab_det.fab_fa_id
               and   faba_fabk_id =  fab_det.fab_fabk_id
               exclusive-lock:
               delete faba_det.
            end. /* for each faba_det */
            if c-application-mode <> "API" then
               hide message no-pause.
            delete fab_det.
            if c-application-mode <> "API" then
            do: 
               clear frame f-1 no-pause.
               get next q_fab_det
                  no-lock.
               if available fab_det
               then do:
                  assign
                     perform-status = "first"
                     l-rowid        = rowid(fab_det).
               end. /* IF AVAILABLE fab_det */
               else 
               do:
                  get prev q_fab_det
                     no-lock.
                  if available fab_det
                  then do:
                     assign
                        perform-status = "first"
                        l-rowid        = rowid(fab_det).
                  end. /* IF AVAILABLE fab_det */
                  else
                     assign
                        perform-status = "first"
                        l-rowid        = rowid(fab_det).
               end. /* ELSE DO */
               /* RECORD DELETED */
               {us/bbi/pxmsg.i &MSGNUM=22 &ERRORLEVEL=1}
            end. /* c-application-mode <> "API" */
            else
               assign
                  perform-status = "next".
            return.
         end. /* when yes */
      otherwise
      run ip-displayedits.
   END CASE.
END PROCEDURE.

PROCEDURE ip-assign:
   if perform-status = "add"
   then do:
      for first fabk_mstr where fabk_mstr.fabk_domain = global_domain and
         fabk_mstr.fabk_id = l-sv-fabkid no-lock:
         for first cu_mstr where cu_mstr.oid_cu_mstr = fabk_mstr.oid_cu_mstr no-lock:
            l-curr = cu_mstr.cu_curr.
         end.
      end.
      /* Get rates from TC to BC, and from TC to SC. */
      {us/gp/gprunp.i "fapl" "p" "fa-assign-rate"
                        "(input l-curr,
                           input fa_mstr.fa_acquis_effectivedate,
                           input base_curr,
                           input l-statutory_curr,
                           input yes,
                           output l-bc-rate,
                           output l-bc-ratescale,
                           output l-sc-rate,
                           output l-sc-ratescale,                       
                           output vErrorNumber)"}
      if vErrorNumber < 0 then
         return.  
   end.
   define input-output parameter perform-status as character no-undo.
   
   define buffer bfabk_mstr for fabk_mstr.
   define buffer bfa_mstr for fa_mstr. 
   define buffer bfad_det for fad_det. 
   define variable delta_cost like fa_mstr.fa_puramt no-undo.   
   define variable l-seq          like fabd_seq      no-undo.
   define variable l_post_yrper   like fabd_yrper    no-undo.
   define variable l_error        as   integer       no-undo.
   define variable l_primarybk    like fabk_id       no-undo.

   
   do with frame f-1:          
      assign 
         fab_det.fab_fabk_id          = l-sv-fabkid
         fab_det.fab_fabk_seq         = l_asd_seq
         fab_det.fab_amt              = l-sv-fabasisamt /* debug l-basis-amt */
         fab_det.fab_fa_id            = fa_mstr.fa_id
         fab_det.fab_mod_userid       = global_userid
         fab_det.fab_mod_date         = today
         fab_det.fab_famtr_id         = l-famtr-id
         fab_det.fab_uplife           = l-uplife
         fab_det.fab_upper            = l-upper
         fab_det.fab_um               = l-um
         fab_det.fab_resrv            = 0
         fab_det.fab_life             = l-sv-fablife /* debug l-fab-life */
         fab_det.fab_famt_id          = l-sv-fabmtid /* debug l-famt-id */
         fab_det.fab_date             = l-sv-fabdate
         fab_det.fab_upcost           = 0
         fab_det.fab_bc_exr_rate      = l-bc-rate
         fab_det.fab_bc_exr_ratescale = l-bc-ratescale
         fab_det.fab_sc_exr_rate      = l-sc-rate
         fab_det.fab_sc_exr_ratescale = l-sc-ratescale 
         /* fab_det.fab_amt              = l-basis-amt */
         l-rowid                      = rowid(fab_det).
      /* --------  AFTER-ASSIGN-AUDIT-INCLUDE  ----------- */
      for first famt_mstr
         where  famt_mstr.famt_domain = global_domain
         and    famt_id               = fab_famt_id
         no-lock:
      end. /* FOR FIRST famt_mstr */
      if available famt_mstr
         and famt_salv
      then do:
         fab_det.fab_salvamt = fa_mstr.fa_salvamt.
         /* fab_det.fab_salvamt =  fa_salvamt / fa_puramt * fab_amt.*/         
      end.
      else
         fab_det.fab_salvamt = 0.
      
      if c-application-mode = "API" and perform-status = "update" 
         and available ttFixedAssetBooks then
         assign fab_det.fab_salvamt = ttFixedAssetBooks.bookSalvamt.

      for first fabk_mstr
         where fabk_mstr.fabk_domain = global_domain
         and   fabk_id                = fab_fabk_id
         no-lock:
      end. /* FOR FIRST fabk_mstr */
      assign
         fab_fabk_seq = if perform-status = "add"
                           and available fabk_mstr
                        then
                           fabk_seq
                        else
                           fab_fabk_seq.
      
      if perform-status = "add" and available fabk_mstr
      then assign fab_post_overridden = fabk_post_overridden.
                           
       /* Once primary book cost or salvage has been changed, change it in the header also */
      for first bfabk_mstr where bfabk_mstr.fabk_domain = global_domain and
        bfabk_mstr.fabk_id = fab_det.fab_fabk_id and
        bfabk_mstr.fabk_primary = yes no-lock: 
            
        for first bfa_mstr 
            where bfa_mstr.fa_domain = global_domain and
                    bfa_mstr.fa_id     = fab_det.fab_fa_id 
            exclusive-lock:

                   
            assign delta_cost          = fab_det.fab_amt - bfa_mstr.fa_puramt
                   bfa_mstr.fa_puramt  = fab_det.fab_amt
                   bfa_mstr.fa_salvamt = fab_det.fab_salvamt.
                   
            for first bfad_det
                where bfad_det.fad_domain = global_domain and
                      bfad_det.fad_fa_id  = fab_det.fab_fa_id 
                exclusive-lock:
                assign bfad_det.fad_puramt = bfad_det.fad_puramt + delta_cost.     
            end.        
        end.                            
      end.                              
      /* ADDED 1ST INPUT PARAMETER AS yes THIS WILL  */
      /* CHECK THAT THE NEXT CALENDAR IS DEFINED FOR */
      /* AN ASSET PLACED IN A SHORT YEAR.            */

      /* ADDED 5TH AND 6TH PARAMETERS AS THE YEAR FOR ADJUSTMENT   */
      /* AND DEPRECIATION CALCULATION FOR FOLLOWING YEARS IS       */
      /* NEEDED OR NOT RESPECTIVELY. THESE PARAMETERS WILL BE zero */
      /* AND no RESPECTIVELY EXCEPT FOR UTILITY utrgendp.p         */
      {us/bbi/gprun.i ""fadpbla.p""
         "(input  yes,
           input  fab_resrv,
           buffer fa_mstr,
           buffer fab_det,
           input  0,
           input  no,
           output l-err-nbr)"}

      if l-err-nbr <> 0
      then do:
         {us/bbi/pxmsg.i &MSGNUM=l-err-nbr &ERRORLEVEL=4}
         perform-status = "undo".
      end.
   /* Exchange rate cannot be zero */
   /*
   if fab_det.fab_bc_exr_rate = 0 or fab_det.fab_bc_exr_rate = ?
      or fab_det.fab_bc_exr_ratescale = 0 or fab_det.fab_bc_exr_ratescale = ?
      or fab_det.fab_sc_exr_rate      = 0 or fab_det.fab_sc_exr_rate = ?
      or fab_det.fab_sc_exr_ratescale = 0 or fab_det.fab_sc_exr_ratescale = ? then
   do:
      {us/bbi/pxmsg.i &MSGNUM=3118 &ERRORLEVEL=4}
      return.
   end.*/

   /* WHEN SECONDARY POSTING BOOK IS MADE 0, THERE NEEDS TO BE ONE */
   /* FABD_DET RECORD TO ENABLE DEPRECIATION DEVIATION POSTING     */
   if l-sv-fabasisamt = 0 
      and available fabk_mstr 
      and fabk_mstr.fabk_primary = false 
      and fabk_mstr.fabk_delta  
   then do:
      if can-find (first bfab_det 
                      where bfab_det.fab_domain  = global_domain 
                        and bfab_det.fab_fa_id   = fa_mstr.fa_id 
		        and bfab_det.fab_fabk_id = l-primary-bk
		        and bfab_det.fab_amt    <> 0 no-lock)
      then do:
         {us/gp/gprunp.i "fapl" "p" "fa-get-per"
              "(input  fab_det.fab_date,
                input  '',
                output l_post_yrper,
                output l_error)"}
         if l_error <> 0
         then
            return.

	 if not can-find (first fabd_det
                             where fabd_det.fabd_domain = global_domain 
                             and   fabd_fa_id           = fa_mstr.fa_id
                             and   fabd_fabk_id         = fabk_mstr.fabk_id
                             and   fabd_yrper           = l_post_yrper
                             and   fabd_entity          = fa_mstr.fa_entity
			     and   fabd_post            = no
                         )
             and fa_mstr.fa_dep = true
         then do:   /* CREATE fabd_det with 0 cost WHEN acquistion has to be posted for the asset book*/
            create fabd_det.
            assign
               fabd_det.oid_fabd_det   = nextOidValue() 
               fabd_det.fabd_domain    = global_domain
               fabd_det.fabd_fa_id     = fa_mstr.fa_id
               fabd_det.fabd_fabk_id   = fabk_mstr.fabk_id
               fabd_det.fabd_entity    = fa_mstr.fa_entity
               fabd_det.fabd_resrv     = 0
               fabd_det.fabd_post      = no
               fabd_det.fabd_yrper     = l_post_yrper         
               fabd_det.fabd_facls_id  = fa_mstr.fa_facls_id
               fabd_det.fabd_faloc_id  = fa_mstr.fa_faloc_id
               fabd_Det.fabd_adj_yrper = l_post_yrper
               fabd_Det.fabd_peramt    = 0
               fabd_Det.fabd_accamt    = 0
               fabd_det.fabd_upper     = 0
               fabd_det.fabd_accup     = 0
               fabd_det.fabd_mod_user  = global_userid
               fabd_det.fabd_mod_date  = today.

               {us/mf/mfnxtsq1.i  "fabd_det.fabd_domain = global_domain and " fabd_det
                  fabd_seq fabd_sq01 l-seq}


               fabd_det.fabd_seq   =  l-seq.
               if recid(fabd_det)  = -1 then.
         end.  /* IF NOT CAN-FIND(FIRST fa_mstr */
      end. /* IF CAN-FIND */
   end.  /* if l-sv-fabasisamt = 0 */
   /* THEN DO: */
   /* ------  END AFTER-ASSIGN-AUDIT-INCLUDE  ----------- */
   end. /* DO WITH frame f-1 */
END PROCEDURE.


PROCEDURE ip-predisplay:
   if (perform-status = ""
      or perform-status = "update")
      and available fab_det
   then
   display-loop:
   do:
      if perform-status = ""
      then do:
         if c-application-mode <> "API" then
            clear frame f-1 all no-pause.
         l-top-rowid = rowid(fab_det).
      end. /* IF perform-status = "" */
      read-loop:
      do pos = 1 to lines:
         if perform-status = "" and c-application-mode <> "API"
         then do:
            if session:display-type = "gui"
            then
               assign
                  fab_fabk_id:bgcolor = 8
                  fab_fabk_id:fgcolor = 0.
            else
               color display normal fab_fabk_id
                  with frame f-1.
         end. /* IF perform-status = "" */
         run ip-displayedits.
         if perform-status = "update"
         then
            leave display-loop.
         if c-application-mode <> "API" then
         do:
            if pos < lines
            then
               down with frame f-1.
            get next q_fab_det no-lock.
            if not available fab_det
            then
               leave.
         end. /* c-application-mode <> "API" */
      end. /* do pos */
      run ip-postdisplay.
   end. /* do */
END PROCEDURE.

PROCEDURE ip-displayedits:
   if available fab_det
   then do:
      /* DISPLAY-EDITS */
      define buffer fabdet for fab_det.

      for last fabd_det
         where fabd_det.fabd_domain = global_domain
         and   fabd_fa_id           = fab_fa_id
         and   fabd_fabk_id         = fab_fabk_id
         and   fabd_post            = yes
         use-index fabd_fa_id
         no-lock:
      end. /* for last fabd_det */
      if available fabd_det
      then
         l-asof-date = fabd_yrper.
      else
         l-asof-date = "".

      assign
         l-famt-id  = ""
         l-fab-life = 0.

      for last fabdet
         where fabdet.fab_domain  = global_domain
         and   fabdet.fab_fa_id   = fab_det.fab_fa_id
         and   fabdet.fab_fabk_id = fab_det.fab_fabk_id
         no-lock:
         assign
            l-famt-id  = fabdet.fab_famt_id
            l-fab-life = fabdet.fab_life.
      end. /* for last fabdet */

      {us/gp/gprunp.i "fapl" "p" "fa-get-cost"
         "(input  fab_fa_id,
           input  fab_fabk_id,
           output l-basis-amt)"}
      
      {us/gp/gprunp.i "fapl" "p" "fa-get-accdep"
         "(input  fab_fa_id,
           input  fab_fabk_id,
           input  l-asof-date,
           output l_dep)"}

      assign
         l_net      = 0
         l_asd_seq  = fab_det.fab_fabk_seq
         l_net      = l-basis-amt - l_dep
         l_dep      = if l_dispose = yes
                      then
                         0
                      else
                         l_dep
         l_net      = if l_dispose = yes
                      then
                         0
                      else
                         l_net
         l-famtr-id = fab_famtr_id
         l-uplife   = fab_uplife
         l-upper    = fab_upper
         l-um       = fab_um.

      for first fabk_mstr where fabk_mstr.fabk_domain = fab_domain and
         fabk_mstr.fabk_id = fab_fabk_id no-lock:
         for first cu_mstr where cu_mstr.oid_cu_mstr = fabk_mstr.oid_cu_mstr no-lock:
            l-curr = cu_mstr.cu_curr.
         end.
      end.
      /* c-application-mode <> "API" */
      if c-application-mode <> "API" then
      do:
         /* rounding format for book currency */
         if l-curr <> '' and l-curr <> ?
         then do:
            {us/gp/gprunp.i "mcpl" "p" "mc-get-rnd-mthd"
            "(input l-curr,
              output l-rnd-mthd,
              output l-err-nbr)"}
            if l-err-nbr <> 0 then 
            do:
               return.
            end.
            {us/bbi/gprun.i ""gpcurfmt.p""
            "(input-output l_net_fmt,
              input        l-rnd-mthd)"}
            {us/bbi/gprun.i ""gpcurfmt.p""
            "(input-output l-basis-amt-fmt,
              input        l-rnd-mthd)"}
         end. 
      end. /* end <> api */
      

      /* DISPLAY-EDITS */ 
      if c-application-mode <> "API" then
         assign 
            l_net:format in frame f-1       = l_net_fmt 
            l-basis-amt:format in frame f-1 = l-basis-amt-fmt.

      run ip-display.
   end. /* IF AVAIL fab_det */
END PROCEDURE.

PROCEDURE ip-display:
   if c-application-mode <> "API" then 
   do:   
      display
         fab_det.fab_fabk_id
         l_net
         l-curr
         l-basis-amt
         l-asof-date
         l-famt-id
         l-fab-life
         fab_det.fab_date
         with frame f-1.
   end. /* c-application-mode <> "API" */
   else 
   do:
      /* debug to-do: check if to remove */
      if perform-status <> "end" then
      do:
         if ttFixedAssetBooks.bookCost = 0 or ttFixedAssetBooks.bookCost = ? then
            assign ttFixedAssetBooks.bookCost = l-basis-amt.
         if ttFixedAssetBooks.bookMethod = '' or ttFixedAssetBooks.bookMethod = ? then
            assign ttFixedAssetBooks.bookMethod = l-famt-id.
         if ttFixedAssetBooks.bookLife = 0 or ttFixedAssetBooks.bookLife = ? then
            assign ttFixedAssetBooks.bookLife = l-fab-life.
         if ttFixedAssetBooks.fabDate = ? then
            assign ttFixedAssetBooks.fabDate = fab_det.fab_date.
         assign
            ttFixedAssetBooks.fabFabkId = fab_det.fab_fabk_id.
      end.
   end.
END PROCEDURE.

PROCEDURE ip-postdisplay:
   if c-application-mode <> "API" then 
   do:
      do pos = 1 to lines:
         if frame-line(f-1) <= 1
         then
            leave.
         up 1 with frame f-1.
      end. /* do pos = 1 to lines */
      if perform-status = ""
      then do:
         reposition q_fab_det to rowid l-top-rowid no-error.
         get next q_fab_det
            no-lock.
         l-rowid = l-top-rowid.
         if p-status <> "first-one"
         then
            color display message fab_fabk_id
   
               with frame f-1.
      end. /* IF perform-status = "" */
   end. /* c-application-mode <> "API" */
END PROCEDURE.

PROCEDURE ip-query:
   define input-output parameter perform-status as character no-undo.
   define input-output parameter l-rowid        as rowid     no-undo.

   
   /* Limit API mode */
   if c-application-mode = "API" then 
   do:
      if perform-status = "first" or
         /* perform-status = "open" or */
         perform-status = "prev" or
         perform-status = "find" then
         perform-status = "next".
   end. /* c-application-mode = "API" */
   if perform-status = "first"
   then do:
      if l-rowid <> ?
      then do:
         reposition q_fab_det to rowid l-rowid
            no-error.
         get next q_fab_det
            no-lock.
      end. /* if l-rowid <> ? */
      if not available fab_det
      then
         get first q_fab_det
            no-lock.
      if available fab_det
      then
         assign
            perform-status = ""
            l-rowid        = rowid(fab_det).
      else 
      do:
         assign
            perform-status    = ""
            l-rowid           = ?
            frame f-1:visible = true.
         return.
      end. /* else do */
   end.  /* IF perform-status = "first" */

   if perform-status = "open"
   then do:
      if c-application-mode <> "API" then 
      do:
         open query q_fab_det
            for each fab_det
            where fab_det.fab_domain = global_domain and  fab_fa_id =
            fa_mstr.fa_id
            and   fab_resrv = 0
            use-index fab_fa_id
            no-lock.
   
         reposition q_fab_det to rowid l-rowid
            no-error.
         get next q_fab_det
            no-lock.
         if available fab_det
         then
            assign
               perform-status = ""
               l-rowid        = rowid(fab_det).
         else 
         do:
            get first q_fab_det
               no-lock.
            if not available fab_det
            then do:
               assign
                  perform-status    = ""
                  frame f-1:visible = true.
               return.
            end. /* if not available */
            else
               assign
                  perform-status = ""
                  l-rowid        = rowid(fab_det).
         end. /* ELSE DO */
      end. /* END <> API */
   end.  /* IF perform-status = "open" */

   if perform-status = "next"
   then do:
      if c-application-mode <> "API" then 
      do:
         get next q_fab_det
            no-lock.
         if available fab_det
         then do:
            hide message no-pause.
            assign
               l-rowid        = rowid(fab_det)
               perform-status = "next".
            if session:display-type = "gui"
            then
               assign
                  fab_fabk_id:bgcolor = 8
                  fab_fabk_id:fgcolor = 0.
            else
               color display normal fab_fabk_id
                  with frame f-1.
            /* MAKES SCROLLING WORK - DON'T REMOVE */
            pause 0.
            down 1 with frame f-1.
            run ip-displayedits.
            color display message fab_fabk_id
               with frame f-1.
         end. /* IF AVAILABLE fab_det */
         else 
         do:
            perform-status = "next".
            /* END OF FILE */
            {us/bbi/pxmsg.i &MSGNUM=20 &ERRORLEVEL=2}
            get last q_fab_det
               no-lock.
         end. /* ELSE DO */
      end. /* c-application-mode <> "API" */
      else 
      do:
         /* Get the Fixed Asset Books record from the API controller */
         run getNextRecord in ApiMethodHandle 
            (input "ttFixedAssetBooks").
      
         if return-value = {&RECORD-NOT-FOUND} then 
         do:
            assign
               perform-status = "end"
               l-rowid        = ?.
            return.
         end.

         if ttFixedAssetBooks.operation = {&ADD} then
            perform-status = "add".
         else if ttFixedAssetBooks.operation = {&MODIFY} then
               perform-status = "update".
            else if ttFixedAssetBooks.operation = {&REMOVE} then
                  perform-status = "delete".
               else if ttFixedAssetBooks.operation = {&GET} then
                     perform-status = "".
                  else 
                     perform-status = "end".
            
         find first fab_det where fab_domain  = global_domain
            and fab_fa_id   = ttFixedAssetBooks.faId
            and fab_fabk_id = ttFixedAssetBooks.fabFabkId
            no-lock no-error.
         /* Default some values in ttFixedAssetBooks, if user not pass these values. */
         if available fab_det then
         do:                
            assign {us/mf/mfaidflt.i ttFixedAssetBooks.bookCost fab_det.fab_amt}.
            if ttFixedAssetBooks.bookMethod = '' or ttFixedAssetBooks.bookMethod = ? then
               assign ttFixedAssetBooks.bookMethod = fab_det.fab_famt_id.
            if ttFixedAssetBooks.bookLife = 0 or ttFixedAssetBooks.bookLife = ? then
               assign ttFixedAssetBooks.bookLife = fab_det.fab_life.
            if ttFixedAssetBooks.fabDate = ? then
               assign ttFixedAssetBooks.fabDate = fab_det.fab_date .
            if fa_mstr.fa_acq_post or ttFixedAssetBooks.bookSalvamt = ? then
               assign ttFixedAssetBooks.bookSalvamt = fab_det.fab_salvamt.
            if fa_mstr.fa_acq_post or ttFixedAssetBooks.fabBCExrRate = 0 or ttFixedAssetBooks.fabBCExrRate = ? then
               assign ttFixedAssetBooks.fabBCExrRate = fab_det.fab_bc_exr_rate.
            if fa_mstr.fa_acq_post or ttFixedAssetBooks.fabSCExrRate = 0 or ttFixedAssetBooks.fabSCExrRate = ? then
               assign ttFixedAssetBooks.fabSCExrRate = fab_det.fab_sc_exr_rate.
         end.            
         run ip-displayedits.
      end. /* c-application-mode = "API" */    
   end.  /* IF perform-status = "next" */

   if perform-status = "prev"
   then do:
      get prev q_fab_det
         no-lock.
      if not available fab_det
      then do:
         /* BEGINNING OF FILE */
         {us/bbi/pxmsg.i &MSGNUM=21 &ERRORLEVEL=2}
         get first q_fab_det
            no-lock.
         return.
      end. /* if not avai fab_det */
      hide message no-pause.
      l-rowid = rowid(fab_det).
      if session:display-type = "gui"
      then
         assign
            fab_fabk_id:bgcolor = 8
            fab_fabk_id:fgcolor = 0.
      else
         color display normal fab_fabk_id
            with frame f-1.
      /* MAKES SCROLLING WORK - DON'T REMOVE */
      pause 0.
      up 1 with frame f-1.
      run ip-displayedits.
      color display message fab_fabk_id
         with frame f-1.
   end.  /* IF perform-status = "prev" */

   if perform-status = "update" or
      perform-status = "delete" or
      ( (perform-status = "" or perform-status = "add") and c-application-mode = "API") then /* debug */
   do:
      if c-application-mode <> "API" then 
      do:
         get current q_fab_det no-lock.
         if not available fab_det
         then do:
            hide message no-pause.
            /* RECORD NOT FOUND */
            {us/bbi/pxmsg.i &MSGNUM=1310 &ERRORLEVEL=3}
            perform-status = "".
         end. /* if not avai fab_det then do.... */
      end. /* c-application-mode <> "API" */
      else 
      do:
         find first fab_det where fab_domain = global_domain and  
            fab_fa_id = fa_mstr.fa_id  and
            fab_resrv = 0 and
            fab_fabk_id = ttFixedAssetBooks.fabFabkId 
            no-lock no-error.
         if not available fab_det then 
         do:
            {us/bbi/pxmsg.i &MSGNUM=1310 &ERRORLEVEL=3}
            perform-status = "end".
         end. /* c-application-mode = "API" */         
      end.
   end. /* if perform-status = "update" or .... */

   if perform-status = "find"
   then do:
      reposition q_fab_det to rowid l-rowid
         no-error.
      get next q_fab_det
         no-lock.
      assign
         l-rowid        = rowid(fab_det)
         perform-status = "".
      run ip-displayedits.
   end. /* IF PERFORM_STATUS = "FIND" */

END PROCEDURE.

PROCEDURE ip-framesetup:
   assign
      frame f-1:row           = 13
      frame f-1:box           = true
      frame f-1:centered      = true
      frame f-1:title         = (getFrameTitle("DEPRECIATION_BOOKS",26))
      frame f-1:title-dcolor  = 8
      frame f-button:centered = true
      frame f-button:row      = 20.
END PROCEDURE.

PROCEDURE ip-button:
   define input-output parameter perform-status as character
      format "x(25)" no-undo.

   if not batchrun and c-application-mode <> "API"
   then do:
      enable all with frame f-button.
      ststatus = stline[2].
      status input ststatus.
      on choose of b-add
         do:
            assign
               perform-status = self:private-data
               l-focus        = self:handle.

            hide frame f-button no-pause.
         end.  /* ON CHOOSE OF b-add */

      on choose of b-delete
         do:
            assign
               perform-status = self:private-data
               l-focus        = self:handle.

            hide frame f-button no-pause.
         end.  /* ON CHOOSE OF b-delete */

      on choose of b-adjust
         do:
            assign
               perform-status = self:private-data
               l-focus        = self:handle
               l-title        = current-window:title
                             + " - "
                             + self:label.
            hide frame f-button no-pause.
         end.  /* ON CHOOSE OF b-adjust */

      on choose of b-accts
         do:
            assign
               perform-status = self:private-data
               l-focus        = self:handle
               l-title        = current-window:title
                  + " - "
                  + self:label.
            hide frame f-button no-pause.
         end.

      on choose of b-detail
         do:

            assign
               perform-status = self:private-data
               l-focus        = self:handle
               l-title        = current-window:title
                             + " - "
                             + self:label.
            hide frame f-button no-pause.
         end.  /* ON CHOOSE OF b-detail */

      on choose of b-audit
         do:

            assign
               perform-status = self:private-data
               l-focus        = self:handle
               l-title        = current-window:title
                             + " - "
                             + self:label.
            hide frame f-button no-pause.
         end.  /* ON CHOOSE OF b-audit  */

      on choose of b-uop
         do:

            assign
               perform-status = self:private-data
               l-focus        = self:handle
               l-title        = current-window:title
                             + " - "
                             + self:label.
            hide frame f-button no-pause.
         end.  /* ON CHOOSE OF b-uop    */

      on choose of b-find
         do:

            assign
               perform-status = self:private-data
               l-focus        = self:handle.

            hide frame f-button no-pause.
         end. /* ON CHOOSE OF B-FIND */

      on choose of b-end
         do:

            assign
               perform-status = self:private-data
               l-focus        = self:handle.

            hide frame f-1 no-pause.
            hide frame f-button no-pause.

            /* ADDED HIDE STATEMENT SO THAT MESSAGE IS HIDDEN */
            /* EVEN WHEN END-ERROR KEY IS PRESSED INSTEAD OF  */
            /* CHOOSING END BUTTON.                           */
            hide message no-pause.
         end. /* ON CHOOSE OF B-END  */

      on cursor-up, f9 anywhere
         do:

            assign
               perform-status = "prev"
               l-focus        = focus:handle.
            apply "go" to frame f-button.
         end.  /* ON CURSOR-UP, F9 ANYWHERE */

      on cursor-down, f10 anywhere
         do:
            assign
               perform-status = "next"
               l-focus        = focus:handle.

            apply "go" to frame f-button.
         end.  /* ON CURSOR-UP, F10 ANYWHERE */

      on cursor-right anywhere
         do:
            assign
               l-focus = self:handle.
            apply "tab" to l-focus.
         end.  /* ON CURSOR-RIGHT ANYWHERE */

      on cursor-left anywhere
         do:
            assign
               l-focus = self:handle.
            if session:display-type = "gui"
            then
               apply "shift-tab" to l-focus.
            else
               apply "ctrl-u" to l-focus.
         end.  /* ON CURSOR-LEFT ANYWHERE */

      on f3 anywhere
         apply "choose" to b-add in frame f-button.
      on f5, ctrl-d anywhere
         apply "choose" to b-delete in frame f-button.
      on esc anywhere
         do:
            if session:display-type = "gui"
            then
               apply "choose" to b-end in frame f-button.
         end. /* ON ESC ANYWHERE */

      on end-error anywhere
         apply "choose" to b-end in frame f-button.
      on window-close of current-window
         apply "choose" to b-end in frame f-button.
      on go anywhere
         do:
            if (lastkey = keycode("cursor-down")
               or lastkey = keycode("cursor-up")
               or lastkey = keycode("f9")
               or lastkey = keycode("f10"))
            then
               return.
            else
               l-focus = focus:handle.
            apply "choose" to l-focus.
         end. /* ON GO ANYWHERE */
      wait-for
         go of frame f-button
         or window-close of current-window
         or choose of
         b-add,
         b-delete,
         b-adjust,
         b-accts,
         b-detail,
         b-audit,
         b-uop,
         b-find,
         b-end
         focus
         l-focus.
      hide message no-pause.

   end. /* IF NOT BATCHRUN */
   else if c-application-mode <> "API" then
         set perform-status.

END PROCEDURE.

PROCEDURE ip-book:

   define input-output parameter l-rowid as rowid no-undo.
   if c-application-mode <> "API" then 
   do:
      prompt-for
         fab_fabk_id
         with frame f-1.
   end. /* c-application-mode <> "API" */
   
   assign
      l-sv-fabkid = if c-application-mode <> "API" then
                    input frame f-1 fab_fabk_id
                 else
                    ttFixedAssetBooks.fabFabkId.
   for first fab_det
      where fab_det.fab_domain = global_domain
      and   fab_fa_id          = fa_mstr.fa_id
      and   fab_fabk_id        = l-sv-fabkid
      no-lock:
   end. /* FOR FIRST fab_det */
    
   if available fab_det
   then
      l-rowid = rowid(fab_det).
   else 
   do:
      /* INVALID BOOK CODE */
      {us/bbi/pxmsg.i &MSGNUM=4214 &ERRORLEVEL=4}
   end. /* ELSE DO */
END PROCEDURE. /* PROCEDURE IP-BOOK */

PROCEDURE ip-delta:

   /* IF PRIMARY POSTING BOOK ADJUSTED THEN CALCULATE DIFFERENCE BETWEEN     */
   /* PRIMARY AND SECONDARY POSTING BOOKS AND POST AMOUNT TO GL IF DELTA YES */

   define input parameter p_delta      like mfc_logical            no-undo.
   define input parameter p-primary-bk like fabk_id                no-undo.
   define input parameter p_fabk_id    like fabk_id                no-undo.
   define input parameter p-fa-id      as character format "x(8)"  no-undo.
   define input parameter p_cost       like fa_puramt              no-undo.
   define buffer bffab_det for fab_det.

   if p-primary-bk = p_fabk_id
   then do:
      for each bffab_det
         where bffab_det.fab_domain  =  global_domain
         and   bffab_det.fab_fa_id   =  p-fa-id
         and   bffab_det.fab_fabk_id <> p-primary-bk no-lock
         break by bffab_det.fab_fabk_id:

         for each fabk_mstr where fabk_mstr.fabk_domain = global_domain
            and fabk_mstr.fabk_id     = bffab_det.fab_fabk_id       
            and fabk_mstr.fabk_delta no-lock:

            if first-of(bffab_det.fab_fabk_id)
            then do:
               find first tt_pstast
                  where tt_pstast.tt_asset = p-fa-id
                  and   tt_pstast.tt_book  = bffab_det.fab_fabk_id
                  no-lock no-error.
               if available tt_pstast
               then do:
                  assign
                     tt_pstast.tt_effdt = today
                     tt_pstast.tt_cost  = tt_pstast.tt_cost + (0 - p_cost).
               end. /* IF AVAILABLE tt_pstast */
               else 
               do:
                  create tt_pstast.
                  assign
                     tt_pstast.tt_asset = p-fa-id
                     tt_pstast.tt_book  = bffab_det.fab_fabk_id
                     tt_pstast.tt_effdt = today
                     tt_pstast.tt_cost  = 0 - p_cost.
               end. /* ELSE DO */
            end. /* IF FIRST-OF(bffab_det.fab_fabk_id) */
         end. /* IF l-DELTA = YES */
      end. /* FOR EACH bffab_det */
   end. /* IF l_delta = yes */

END PROCEDURE. /* PROCEDURE ip-delta */
